// pages/repairPart/repairDetail.js
import config from "../../config.js";

const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseKeeperId: '',
    locLatitudePick: '',
    locLongitudePick: '',
    locLatitudeReturn: '',
    locLongitudeReturn: '',
    addressPick: '',
    addressReturn: '',
    serviceType: '',
    tel: '',
    pickTime: '',
    returnTime: '',
    returnTime: '',
    returnTime: '',
    isNeed: '',
    stepcar: '',
    mark: '',
    defaultKeeper: {
      name: '',
      header: '',
      houseKeeperId: null
    },
    repairItemsList: [],
    isAgain: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("enter");
    var that = this;
    var isAgain = options.isAgain;
    if (isAgain != null) {
      that.setData({
        isAgain: isAgain
      })
    }


    that.getRepairItemList();
    this.data.houseKeeperId = options.keeperId != null ? options.keeperId : -1;
    this.data.locLatitudePick = wx.getStorageSync('getLatitude');
    this.data.locLongitudePick = wx.getStorageSync('getLongitude');
    this.data.locLatitudeReturn = wx.getStorageSync('backLatitude');
    this.data.locLongitudeReturn = wx.getStorageSync('backLongitude');
    this.data.addressPick = wx.getStorageSync('getAddress');
    this.data.addressReturn = wx.getStorageSync('backAddress');
    this.data.serviceType = wx.getStorageSync('serviceType');
    this.data.tel = wx.getStorageSync('tel');
    this.data.pickTime = wx.getStorageSync('timePick');
    this.data.returnTime = wx.getStorageSync('timeReturn');
    this.data.mark = wx.getStorageSync('mark');
    this.data.isNeed = wx.getStorageSync('isNeed');
    this.data.stepcar = wx.getStorageSync('stepcar');

    if (wx.getStorageSync('user') != null) {
      that.setData({
        'defaultKeeper.name': wx.getStorageSync("user").housekeeper.name,
        'defaultKeeper.header': wx.getStorageSync("user").housekeeper.image,
        'defaultKeeper.houseKeeperId': options.keeperId != null ? options.keeperId : -1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    wx.setStorageSync("orderConfirmType", null);
    // wx.setStorageSync("orderId", null);
    var user = wx.getStorageSync("user");
    if (this.data.houseKeeperId == "") {
      this.data.houseKeeperId = that.data.defaultKeeper.houseKeeperId;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  checkboxAction: function(e) {
    var item = e.currentTarget.dataset.item;
    var numId = e.detail.value;
    console.log(numId);

    item.numId = numId;

    for (var i = 0; i < this.data.repairItemsList.length; i++) {
      var subItem = this.data.repairItemsList[i];
      for (var j = 0; j < subItem.repairPartSubs.length; j++) {
        var it = subItem.repairPartSubs[j];
        if (it.subId == item.subId) {
          it.numId = numId;
          subItem.repairPartSubs[j] = it;
          this.data.repairItemsList[i].repairPartSubs = subItem.repairPartSubs;
        }
      }

    }

    console.log(this.data.repairItemsList);
  },
  getRepairItemList: function() {
    var that = this;

    wx.showLoading({
      title: "正在获取数据..."
    })
    wx.request({
      url: base.repairItemsList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {},
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          that.setData({
            repairItemsList: res.data.data,
          })
          if (that.data.isAgain == 1) {
            var againList = wx.getStorageSync("reqairListAgain");
            for (var k = 0; k < againList.length; k++) {
              var sub = againList[k];
              var item = sub.repairPartSub;
              for (var i = 0; i < that.data.repairItemsList.length; i++) {
                var subItem = that.data.repairItemsList[i];
                for (var j = 0; j < subItem.repairPartSubs.length; j++) {
                  var it = subItem.repairPartSubs[j];
                  if (it.subId == item.subId) {
                    if (item.subId == it.subId && sub.category != " ") {
                      var catgory = sub.category.trim();
                      var cats = catgory.split(" ");
                      var nums = [];
                      switch (cats.length) {
                        case 1:
                          nums = ["1"]
                          break;
                        case 2:
                          nums = ["1", "2"]
                          break;
                        case 3:
                          nums = ["1", "2", "3"]
                          break;
                        default:
                          nums = []
                          break;
                      }
                      it.numId = nums;
                    } else {
                      it.numId = [];
                    }

                    subItem.repairPartSubs[j] = it;
                    that.data.repairItemsList[i].repairPartSubs = subItem.repairPartSubs;
                    break;
                  }
                }

              }
            }
            that.setData({
              repairItemsList: that.data.repairItemsList
            })
            console.log(that.data.repairItemsList);
          }
        } else {
          wx.showToast({
            title: "数据获取失败!",
            icon: "none"
          })
        }
      }
    })
  },
  contact: function(e) {
    console.log(e);
    wx.makePhoneCall({
      phoneNumber: '400-9988960',
    })
  },
  submitAction: function(e) {
    var that = this;


    var orderType = 4;
    var userId = wx.getStorageSync("userId");
    var carId = wx.getStorageSync("carId");
    var orderId = wx.getStorageSync("orderId");
    var comId = wx.getStorageSync("comId");
    var repaireType = wx.getStorageSync("repaireType");
    var orderFrom = wx.getStorageSync("orderFrom");
    var undetermined = wx.getStorageSync("undetermined");
    var determined = undetermined == true ? 1 : 0;

    var licenseFr = wx.getStorageSync("licenseFr");
    var licenseBa = wx.getStorageSync("licenseBa");
    var idenCardA = wx.getStorageSync("idenCardA");
    var idenCardB = wx.getStorageSync("idenCardB");
    if (orderId != null && orderId == "") {
      orderId = null;
    }

    console.log("订单编号:" + orderId);
    //使用json格式传输数据
    var jsonstrs = JSON.stringify(this.data.repairItemsList);
    jsonstrs = '{"data":' + jsonstrs + '}';
    //console.log("输出" + jsonstrs);
    wx.request({
      url: base.repairSubbmit,
      data: {
        userId: userId,
        orderId: orderId == null ? -1 : orderId,
        carId: carId,
        comId: comId,
        undetermined: determined,
        orderType: orderType,
        orderFrom: orderFrom,
        repaireType: repaireType,
        isNeedStepCar: that.data.isNeed,
        stepCarId: that.data.stepcar,
        serviceType: this.data.serviceType,
        houseKeeperId: this.data.houseKeeperId,
        locLatitudePick: this.data.locLatitudePick,
        locLongitudePick: this.data.locLongitudePick,
        locLatitudeReturn: this.data.locLatitudeReturn,
        locLongitudeReturn: this.data.locLongitudeReturn,
        addressPick: that.data.addressPick,
        addressReturn: that.data.addressReturn,
        tel: that.data.tel,
        pickTime: that.data.pickTime,
        returnTime: that.data.returnTime,
        mark: that.data.mark,
        items: jsonstrs,
        licenseFr: licenseFr,
        licenseBa: licenseBa,
        idenCardA: idenCardA,
        idenCardB: idenCardB,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          var orderId = res.data.data.oid;
          wx.setStorageSync("orderConfirmType", 1);
          wx.setStorageSync("orderId", orderId);
          var orderId = wx.getStorageSync("orderId");
          if (orderFrom == 1) {
            wx.navigateTo({
              url: "../orderSuccess/success?id=" + orderId,
              complete: function() {
                wx.setStorageSync("orderConfirmType", null);
                wx.setStorageSync("orderId", null);
              }
            })
          } else {
            wx.setStorageSync("orderConfirmType", 1);
            wx.reLaunch({
              url: "../gjIndex/gjorder/gjorder",
            })
          }

          // wx.showModal({
          //   title: "恭喜您",
          //   content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
          //   showCancel: false,
          //   confirmColor: "#137AF0",
          //   success: function() {



          //   }
          // })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  }
})